using System;
using Didsoft.Silverlight.ViewModel.Data.Contracts;
using Didsoft.Silverlight.ViewModel.Data;
using SIOGDE.Silverlight.Entities;
using SIOGDE.Silverlight.WCFClient.CategoriaUnidadCliente;

namespace SIOGDE.Silverlight.DataService
{
    public partial class CategoriaUnidadDataService : IDataModelBase<CategoriaUnidad>
    {
	   private CategoriaUnidadWcfServicioClient service = new CategoriaUnidadWcfServicioClient();

       public event EventHandler<SaveEntityEventArgs<CategoriaUnidad>> OnEntitySaveCompleted;
        public event EventHandler<DeleteEntityEventArgs> OnEntityDeleteCompleted;
        public event EventHandler<GetUniqueEntityEventArgs<CategoriaUnidad>> OnEntityGetCompleted;
        public event EventHandler<GetAllEntitiesEventArgs<CategoriaUnidad>> OnEntityGetAllCompleted;
		public event EventHandler<SearchByFilterEventArgs<CategoriaUnidad>> OnSearchByFilterCompleted;

        #region Deleting

        public void Delete(CategoriaUnidad entity)
        {
            service.EliminarCompleted += service_EliminarCompleted;
            service.EliminarAsync(entity);
        }

        void service_EliminarCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            service.EliminarCompleted -= service_EliminarCompleted;
            if (e.Error != null) // mostrar error
                Didsoft.Silverlight.ContentManager.ExceptionMessage.Show(e.Error.Message);

            if (OnEntityDeleteCompleted != null)
                OnEntityDeleteCompleted(this, new DeleteEntityEventArgs(e.Error == null ? true : false));
        }

        #endregion

        #region Saving

        public void Save(CategoriaUnidad entity)
        {
            service.GuardarCompleted += service_GuardarCompleted;
            service.GuardarAsync(entity);
        }

        void service_GuardarCompleted(object sender, GuardarCompletedEventArgs e)
        {
            service.GuardarCompleted -= service_GuardarCompleted;
            if (e.Error != null)
                Didsoft.Silverlight.ContentManager.ExceptionMessage.Show(e.Error.Message);

            if (OnEntitySaveCompleted != null)
                OnEntitySaveCompleted(this, new SaveEntityEventArgs<CategoriaUnidad>(e.Error==null?e.Result:null, e.Error==null?true:false));
        }        

        #endregion
        
        #region GetAll

        public void GetAll()
        {
            service.ObtenerTodasCompleted += service_ObtenerTodasCompleted;
            service.ObtenerTodasAsync();
        }

        void service_ObtenerTodasCompleted(object sender, ObtenerTodasCompletedEventArgs e)
        {
            service.ObtenerTodasCompleted -= service_ObtenerTodasCompleted;
            if (e.Error != null) 
                Didsoft.Silverlight.ContentManager.ExceptionMessage.Show(e.Error.Message);

            if (OnEntityGetAllCompleted != null)
                OnEntityGetAllCompleted(null, new GetAllEntitiesEventArgs<CategoriaUnidad>(e.Error == null ? e.Result : null, e.Error == null ? true : false));
        }

        #endregion

        #region Get

        public void Get(object parameter)
        {
            service.ObtenerPorIdCompleted += service_ObtenerPorIdCompleted;
            service.ObtenerPorIdAsync(parameter);
        }

        void service_ObtenerPorIdCompleted(object sender, ObtenerPorIdCompletedEventArgs e)
        {
            service.ObtenerPorIdCompleted -= service_ObtenerPorIdCompleted;
            if (e.Error != null) 
                Didsoft.Silverlight.ContentManager.ExceptionMessage.Show(e.Error.Message);

            if (OnEntityGetCompleted != null)
                OnEntityGetCompleted(null, new GetUniqueEntityEventArgs<CategoriaUnidad>(e.Error == null ? e.Result : null, e.Error == null ? true : false));
        }

        #endregion  
		
		#region Search By filter

        public void SearchByFilter(string parameter)
        {
            service.BuscarPorFiltroCompleted += service_BuscarPorFiltroCompleted;
            service.BuscarPorFiltroAsync(parameter);
        }

        void service_BuscarPorFiltroCompleted(object sender, BuscarPorFiltroCompletedEventArgs e)
        {
            service.BuscarPorFiltroCompleted -= service_BuscarPorFiltroCompleted;
            if (e.Error != null)            
                Didsoft.Silverlight.ContentManager.ExceptionMessage.Show(e.Error.Message);
            
            if(OnSearchByFilterCompleted!=null)
                OnSearchByFilterCompleted(null,new SearchByFilterEventArgs<CategoriaUnidad>(e.Result,e.Error == null ? true : false));
        }

        #endregion
		    
    }
}


